package com.huiyujiaoyu.powertime.ui.column;


import android.os.Bundle;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.huiyujiaoyu.base.AppContext;
import com.huiyujiaoyu.base.BaseFragment;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.IndexItemModel;
import com.huiyujiaoyu.powertime.model.SimpleListBean;
import com.huiyujiaoyu.powertime.model.TabEntity;
import com.huiyujiaoyu.powertime.ui.adapter.AllFragmentPager2Adapter;
import com.huiyujiaoyu.widget.ForbidenScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.huiyujiaoyu.base.AppContext.datas;


/**
 * 首页
 * Created by yujunlong on 2016/12/5.
 */

public class ColumnFragment extends BaseFragment {

    @BindView(R.id.tab)
    SlidingTabLayout mTab;
    @BindView(R.id.view_pager_2)
    ForbidenScrollViewPager mViewPager2;
    private List<BaseFragment> mBaseFragments;
    private AllFragmentPager2Adapter adapter;
    private String[] titles;
    public static ColumnFragment newInstance() {
        Bundle args = new Bundle();
        ColumnFragment fragment = new ColumnFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_column;
    }

    @Override
    protected void userBundle(Bundle savedInstanceState) {
        super.userBundle(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        mViewPager2.setExpenseOnTouch(true);
        mViewPager2.setForbidenScroll(true);
        mViewPager2.setOffscreenPageLimit(4);

        AppContext.datas.clear();
        if(datas.size()>0){
            setData();
        }else{
            HttpMethods.getInstance().column(mContext,getComp(),this,"0");
        }
        setTitleNoBar();

    }


    @Override
    protected void initListener() {
        super.initListener();
        mTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager2.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_COL_INDEX:
                SimpleListBean bean = (SimpleListBean) o;
                IndexItemModel modle = new IndexItemModel();
                modle.dict_key = 0;
                modle.dict_value = "全部分类";
                AppContext.datas.add(modle);
                AppContext.datas.addAll(bean.dict);
                setData();
                break;
        }
    }

    private void setData(){
        mBaseFragments = new ArrayList<>();
        int size =AppContext.datas.size();
        titles = new String[size];
        for (int i = 0; i < size; i++) {
            titles[i] = AppContext.datas.get(i).dict_value;
            mBaseFragments.add(Column2Fragment.newInstance(AppContext.datas.get(i).dict_key));
        }
        adapter = new AllFragmentPager2Adapter(getChildFragmentManager(),mBaseFragments);
        mViewPager2.setAdapter(adapter);
        mTab.setViewPager(mViewPager2,titles);
        mTab.setCurrentTab(0);
    }
}
